<template>
<div>
	<content_top title="分摊列表" isAccounts="1" @sendMonthDate="getMonthDate"></content_top>
	
	<div class="g_box">
		<div class="g_head">
			<h3>筛选查询</h3>
			<div class="g_box_right">
				<ButtonGroup class="g_act_box">
					<Button @click="query">查询结果</Button>
				</ButtonGroup>
				<div class="g_other">
					<a @click="clear_filter">清除筛选</a>
				</div>
			</div>
		</div>
		<div class="g_box_con" style="padding:20px;">
			<Form  :label-width="110" style="width: 100%" inline>
				<FormItem label="类型：">
					<Select v-model="filter.type" style="width:150px" placeholder="请选择所在地">
						<Option :value="-1" >全部</Option>
						<Option :value="0" >现金</Option>
						<Option :value="1" >银行</Option>
						<Option :value="2" >其他</Option>
					</Select>
				</FormItem>
				<FormItem label="承担部门：">
					<bumen_filter v-model="filter.bumen" field="name" style="width:150px;"></bumen_filter>
				</FormItem>
				<FormItem label="费用科目：">
					<subject_filter v-model="filter.subject" field="name" style="width:150px;"></subject_filter>
				</FormItem>
				<FormItem label="币种：">
					<currency_filter v-model="filter.bz" field="name" style="width:150px;"></currency_filter>
				</FormItem>
				<FormItem label="时间：">
					<!-- <DatePicker :value="filter.date_month" type="month" placeholder="选择月份" style="width: 150px" @on-change="month_change"></DatePicker> -->
					<DatePicker :value="filter.start_date_month" type="month" placeholder="选择月份" style="width:100px" @on-change="month_change"></DatePicker>至
					<DatePicker :value="filter.end_date_month" type="month" placeholder="选择月份" style="width:100px" @on-change="end_month_change"></DatePicker>
				</FormItem>
				
				<FormItem label="关键词搜索：">
					<Input v-model="filter.keyword" placeholder="请输入关键词" style="width: 150px"></Input>
				</FormItem>
			</Form>
		</div>
	</div> 
	
	<div class="g_box">
		<div class="g_head">
			<h3>数据列表</h3>
			<div class="g_box_right">
				<ButtonGroup class="g_act_box">
					<Button @click="import_entry">引入记账</Button>
				</ButtonGroup>
				<div class="g_other">
					
				</div>
			</div>
		</div>
		<div class="g_box_con">
			<Table ref="table" :columns="Columns" :data="list" :height="500" :border="true"></Table>
			<div style="font-size: 14px;overflow: hidden;padding: 10px;">
				<div style="float:left;">
					<Select v-model="batch_act" style="width:100px;;">
						<Option value="" >请选择</Option>
						<Option value="compare">对账</Option>
						<Option value="qcompare">取消对账</Option>
						<Option value="delete" >删除</Option>
					</Select>
					<Button @click="batch_action">确定</Button>
				</div>
				<div style="float:right;">
					<Page :total="count" show-elevator show-total @on-change="goto_page" />
				</div>
			</div>
		</div>
	</div>
	<!-- 引入入账 -->
	<yinru_box v-model="yinru.show" title="引入入账" :rzInfo="yinru.info" @success="info_success"></yinru_box>
	<!-- 新增 -->
	<info_box v-model="einfo.show" :isEdit="isEdit" :cinfo="einfo.info" :date_month="einfo.date_month" @success="info_success"></info_box>

</div>
</template>

<script>
import content_top from '@/themes/components/content_top';
import info_box from './info';
import yinru_box from './yinru';

import bumen_filter from '@/themes/components/bumen_filter';
import subject_filter from '@/themes/components/subject_filter';
import currency_filter from '@/themes/components/currency_filter';
export default {
    name: 'accounts_list',
    data () {
		let self = this;
        return {
        	isEdit: false,
            Columns: [
				{
					type: 'selection',
					width: 60,
					align: 'center'
				},
				{
					title: '类型',
					key: 'type',
					align: 'center',
                    width: 80,
					render: (h, params) => {
						var arr = ['现金','银行','其他'];
						return h('div',arr[params.row.type]);
					}
                },
				{
                    title: '日',
					key: 'day',
					align: 'center',
					width: 60,
                },
				{
                    title: '月',
					key: 'month',
					align: 'center',
					width: 60,
                },
				{
                    title: '承担部门',
					key: 'bill_num',
					align: 'center',
					width: 90,
					render: (h, params) => {
						
						var bumen_fg = params.row.bumen.split(',');
						var scale_fg = params.row.bumen_scale.split(',');
						if(bumen_fg.length>1){
							var ls = [];
							for(var i=0;i<bumen_fg.length;i++){
								ls.push(h('p',bumen_fg[i]+' '+scale_fg[i]+'%'));
							}
						}
						else{
							var ls = params.row.bumen;
						}
						return h('div', ls);
					}
					
                },
				{
                    title: '收入',
					key: 'income_s',
					align: 'center',
					width: 110,
                },
				{
                    title: '支出',
					key: 'expenses_s',
					align: 'center',
					width: 110,
                },
				{
                    title: '结余',
					key: 'balance',
					align: 'center',
					width: 90,
                },
				{
					title: '摘要',
					align: 'center',
					key: 'info',
					minWidth: 70,
                },
				{
                    title: '费用科目',
					key: 'subject',
					align: 'center',
					width: 150,
                },
				{
                    title: '凭证编号',
					key: 'certificate',
					align: 'center',
					width: 140,
                },
				{
					title:'操作',
					key: 'action',
					width:220,
					align: 'center',
					render: (h, params) => {
						
						return h('div', [
							h('Button', {
								props: {
									type: params.row.status==0?'info':'dashed',
									size: 'small',
								},
								style: {
									marginRight: '5px',
								},
								on: {
									click: () => {
										self.set_ok(params.row.id,params.row.status);
										/*if(params.row.status==0){
											self.set_ok(params.row.id)
										}*/
									}
								}
							}, params.row.status==0?'对账':'已对'),
							h('Button', {
								props: {
									type: 'info',
									size: 'small',
								},
								style: {
									marginRight: '5px',
								},
								on: {
									click: () => {
										this.dbedit(params.row,params.row.id)
									}
								}
							}, '编辑'),
							h('Button', {
								props: {
									type: 'info',
									size: 'small',
								},
								style: {
									marginRight: '5px',
								},
								on: {
									click: () => {
										self.remove(params.row.id)
									}
								}
							}, '删除')
						]);
					}
				}
            ],
			filter:{
				type:-1,
				start_date_month:localStorage.account_balance?localStorage.account_balance:new Date().Format('yyyy-MM'),
				end_date_month:localStorage.account_balance?localStorage.account_balance:new Date().Format('yyyy-MM'),
				date_month:localStorage.account_balance?localStorage.account_balance:new Date().Format('yyyy-MM'),//当前期
				keyword:'',
				region:'',
				bumen:'',
				subject:'',
				bz:'all',
			},
			einfo:{
				show:false,
				info:{},
				date_month:'',
			},
			yinru:{
				show:false,
				info:{},
			},
			imports:{
				show:false,
			},
			ruleValidate:{
				month:[
					{ required: true, message: '导入时间范围不能为空', trigger: 'blur' }
				]
			},
			list:[],
			page:1,
			count:0,
			batch_act:'',
			balance_status:0,
        };
	},
	components:{
		content_top,
		info_box,
		yinru_box,
		bumen_filter,
		subject_filter,
		currency_filter
	},
	created(){
		
		this.load_data();
	},
    computed: {
       
    },
	methods:{
		load_data(){

			var self = this;
			var postdata = {
				f:this.filter,
				page:this.page
			};
			
			Whttp.load('Accounting/TeamCash/get_list',postdata,function(result){//console.log(result);return false;
				self.list = result.list;
				self.count = parseInt(result.count);
				self.balance_status = result.balance_status;
			})
		},
		goto_page(page){
			this.page = page;
			this.load_data();
		},
		query(){
			this.page = 1;
			this.load_data();
		},
		// 引入入账
		import_entry(){
			if(this.balance_status){
				alert('当前月份已经结账');
				return;
			}
			
			this.yinru.id = 0;
			this.yinru.info = {};
			this.yinru.show = true;
		},
		month_change(date){
			//this.filter.date_month =  date;
			if(this.filter.end_date_month){
				if(date > this.filter.end_date_month){
					alert('开始月份不能大于截止月份');
					return ;
				}else{
					this.filter.start_date_month =  date;
				}
			}else{
				this.filter.start_date_month =  date;
			}
		},
		end_month_change(date){
			//if(date < this.filter.date_month){alert('截止月份不能小于开始月份')}else{alert('成功')}return false;
			if(date){
				if(date < this.filter.start_date_month){
					alert('截止月份不能小于开始月份');
					return ;
				}else{
					this.filter.end_date_month =  date;
				}
			}
			//this.filter.end_date_month =  date;
		},

		//编辑
		dbedit(row,index){
			if(this.balance_status){
				alert('当前月份已经结账');
				return;
			}
			this.einfo.id = row.id;
			this.einfo.date_month = row.date_month;
			this.einfo.info = this._getData(row);
			this.einfo.show = true;
			this.isEdit = true;
			
		},
		info_success(){
			this.filter.type = -1;
			this.einfo.show = false;
			this.page = 1;
			this.load_data();
		},
		clear_filter(){
			this.filter = this._getData(this.default_filter);
			this.page = 1;
			this.load_data();
		},
		remove(id){
			if(this.balance_status){
				alert('当前月份已经结账');
				return;
			}
			
			var self = this;
			if(!confirm('是否删除')){
				return false;
			}
			var postdata = {
				id:id
			};
			Whttp.load('Accounting/Cash/del',postdata,function(result){//console.log(result);return false;
				self.load_data();
				self.$Message.info('删除成功');
			});
		},
		batch_action(){
			if(this.balance_status){
				alert('当前月份已经结账');
				return;
			}
			
			var ids = [];
			if(this.batch_act=='delete'){
				if(!confirm('是否批量删除')){
					return false;
				}
			}
			
			var sels = this.$refs['table'].getSelection();
			
			for(var i=0;i<sels.length;i++){
				ids.push(sels[i].id);
			}
			
			var self = this;
			var postdata = {
				ids:ids,
				act:this.batch_act
			};
			Whttp.load('Accounting/Cash/batch_act',postdata,function(result){//console.log(result);return false;
				self.load_data();
				self.$Message.info('操作成功');
				self.batch_act = '';
			});
			
			
		},
		set_ok(id,code){
			if(this.balance_status){
				alert('当前月份已经结账');
				return;
			}
			if(code == 1){
				var msg = "取消对账";
				var tisi = "是否设置取消对账";
				var status = 0;
			}else{
				var msg = "对账成功";
				var tisi = "是否设置已对账";
				var status = 1;
			}
			var self = this;
			if(!confirm(tisi)){
				return false;
			}
			var postdata = {
				id:id,
				status:status
			};
			Whttp.load('Accounting/Cash/set_ok',postdata,function(result){
				self.load_data();
				self.$Message.info(msg);
			});
		},
		getMonthDate(val){
			this.filter.date_month = val;
		}
	}
};
</script>
<style scoped>

</style>